/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* inorderSuccessor(TreeNode* u, TreeNode* p) {
        if(u==nullptr) return nullptr;
        TreeNode *ans=nullptr;
        while(u)
        {
            if(u->val>p->val) {ans=u; u=u->left;}
            else u=u->right; 
        }
        return ans;
    }
};